package com.esms.bean;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 * A data access object (DAO) providing persistence and search support for
 * Person entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.esms.bean.Person
 * @author MyEclipse Persistence Tools
 */

public class PersonDAO extends BaseHibernateDAO {
	private PersonDAO() {}
	public static PersonDAO $ = new PersonDAO(); 
	private static final Log log = LogFactory.getLog(PersonDAO.class);
	// property constants
	public static final String USERID = "userid";
	public static final String PASSWORD = "password";
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String BIRTHDAY = "birthday";
	public static final String EMAIL = "email";
	public static final String MOBILE = "mobile";
	public static final String CREATOR = "creator";

	public void save(Person transientInstance) {
		log.debug("saving Person instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	//add by Jian
	public void insert(Person person) {
	    Session session = HibernateSessionFactory.getSession();  
	    Transaction tx = session.beginTransaction();  
	    try {  
	        session.save(person);  
	        tx.commit();  
	    } catch (Exception e) {  
	        tx.rollback();  
	        e.printStackTrace();  
	    }finally{  
	        session.close();  
	    }  
	}
	
	public void update(Person person) {
	    Session session = HibernateSessionFactory.getSession();  
	    Transaction tx = session.beginTransaction();  
	    try {  
	        session.update(person);  
	        tx.commit();  
	    } catch (Exception e) {  
	        tx.rollback();  
	        e.printStackTrace();  
	    }finally{  
	        session.close();  
	    }  
	}
	
	public void delete(Person person) {
	    Session session = HibernateSessionFactory.getSession();  
	    Transaction tx = session.beginTransaction();  
	    try {  
	        session.delete(person);  
	        tx.commit();  
	    } catch (Exception e) {  
	        tx.rollback();  
	        e.printStackTrace();  
	    }finally{  
	    	//session.flush();
	        session.close();  
	    }  
	}
	
	public Person findById(java.lang.Integer id) {
		log.debug("getting Person instance with id: " + id);
		try {
			Person instance = (Person) getSession().get("com.esms.bean.Person",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Person instance) {
		log.debug("finding Person instance by example");
		try {
			List results = getSession().createCriteria("com.esms.bean.Person")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Person instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Person as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByBirthday(Object birthday) {
		return findByProperty(BIRTHDAY, birthday);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByMobile(Object mobile) {
		return findByProperty(MOBILE, mobile);
	}

	public List findByCreator(Object creator) {
		return findByProperty(CREATOR, creator);
	}

	public List findAll() {
		log.debug("finding all Person instances");
		try {
			String queryString = "from Person";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Person merge(Person detachedInstance) {
		log.debug("merging Person instance");
		try {
			Person result = (Person) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Person instance) {
		log.debug("attaching dirty Person instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Person instance) {
		log.debug("attaching clean Person instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}